package com.atguigu.sparkcore.day03.ser

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * Author atguigu
 * Date 2020/10/30 10:10
 */
object SerDemo {
    def main(args: Array[String]): Unit = {
        
        //1.创建SparkConf并设置App名称
        val conf: SparkConf = new SparkConf().setAppName("SparkCoreTest").setMaster("local[*]")
        
        //2.创建SparkContext，该对象是提交Spark App的入口
        val sc: SparkContext = new SparkContext(conf)
        
        //3.创建一个RDD
        val rdd: RDD[String] = sc.makeRDD(Array("hello world", "hello spark", "hive", "atguigu"))
        
        //3.1创建一个Search对象
        val search = new Search("hello")
        
        val rdd2: RDD[String] = search.getMatch1(rdd)
        
        rdd2.collect.foreach(println)
        //4.关闭连接
        sc.stop()
    }
}

class Search(query: String) extends Serializable {
    
    def isMatch(s: String): Boolean = {
        s.contains(query)
    }
    
    def getMatch1(rdd: RDD[String]): RDD[String] = {
        //        rdd.filter(isMatch)
        //rdd.filter(x => x.contains(query))
        
        val q = this.query
        rdd.filter(x => x.contains(q))
    }
    
}

